package org.qydjk.console.service.impl;

import org.qydjk.common.pojo.Area;
import org.qydjk.common.service.impl.BaseService;
import org.qydjk.common.util.ListUtil;
import org.qydjk.console.service.IAreaService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AreaService extends BaseService implements IAreaService {
    /**
     * 增加区域时修改父节点为非叶子节点
     *
     * @param area
     * @throws Exception
     */
    public void saveAreaAndUpdateParentLeaf(Area area) throws Exception {
        area.setIsLeaf(1);
        String parentAreaFullName = findAreaFullNameById(area.getParentId());
        area.setFullName(parentAreaFullName + area.getName());
        baseDao.save(area);
        if (area.getParentId() != null) {
            Area parentArea = baseDao.get(Area.class, area.getParentId());
            if (parentArea.getIsLeaf() == 1) {
                parentArea.setIsLeaf(0);
                baseDao.saveOrUpdate(parentArea);
            }
        }
    }

    /**
     * 刪除区域时如果父节点不包含子节点,修改父节点为叶子节点
     *
     * @param area
     */
    public void deleteAreaAndUpdateParentLeaf(Area area) throws Exception {
        Area areaInfo = baseDao.get(Area.class, area.getId());
        baseDao.delete(areaInfo);
        List<Area> parentAreaList = baseDao.findByHql("FROM Area a WHERE a.parentId = " + areaInfo.getParentId());
        if (ListUtil.isNullOrEmpty(parentAreaList)) {
            Area parentArea = baseDao.get(Area.class, areaInfo.getParentId());
            parentArea.setIsLeaf(1);
            baseDao.saveOrUpdate(parentArea);
        }
    }

    /**
     * 编辑区域,判断是否需要更新父节点叶子状态
     */
    public void updateAreaAndUpdateParentLeaf(Area area, Integer parentId) throws Exception {
        String parentAreaFullName = findAreaFullNameById(area.getParentId());
        area.setFullName(parentAreaFullName + area.getName());
        baseDao.update(area);
        //检查原父节点是否变为叶子节点
        List<Area> parentAreaList = baseDao.findByHql("FROM Area a WHERE a.parentId = " + parentId);
        if (ListUtil.isNullOrEmpty(parentAreaList)) {
            Area parentArea = baseDao.get(Area.class, parentId);
            parentArea.setIsLeaf(1);
            baseDao.saveOrUpdate(parentArea);
        }
        //新父节点变更为非叶子节点
        if (area.getParentId() != null) {
            Area parentArea = baseDao.get(Area.class, area.getParentId());
            if (parentArea.getIsLeaf() == 1) {
                parentArea.setIsLeaf(0);
                baseDao.saveOrUpdate(parentArea);
            }
        }
    }

    /**
     * 递归查询全称
     *
     * @param id
     * @return
     * @throws Exception
     */
    public String findAreaFullNameById(Integer id) throws Exception {
        if (id != null) {
            Area area = baseDao.get(Area.class, id);
            String name = area.getName();
            Integer parentId = area.getParentId();
            if (parentId != null) {
                String nameIn = findAreaFullNameById(parentId);
                name = nameIn + name;
                return name;
            } else {
                return name;
            }
        } else {
            return "";
        }
    }
}
